package com.quickcancellation.crm.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.query.MPJQueryWrapper;
import com.quickcancellation.crm.pojo.dto.PageParamsDto;
import com.quickcancellation.crm.pojo.entity.Product;
import com.quickcancellation.crm.mapper.ProductMapper;
import com.quickcancellation.crm.service.IProductService;
import com.quickcancellation.crm.pojo.vo.ProductVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2023-02-04
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Resource
    private ProductMapper productMapper;

    @Override
    public IPage<Product> selectProductList(PageParamsDto<Product> params) {
        MPJQueryWrapper wrapper = new MPJQueryWrapper<ProductVo>()
                .selectAll(ProductVo.class)
                .select("t.id",
                        "t.productCode",
                        "t.productName",
                        "t.brandId",
                        "t.batchUnit",
                        "t.singleUnit",
                        "t.batchCount",
                        "t.price",
                        "t.productItem",
                        "t.status",
                        "t.remark",
                        "t.createOp",
                        "t.createTime",
                        "t.updateOp",
                        "t.updateTime",
                        "dic.dicVal as statusName",
                        "pd.itemName"
                )
                .leftJoin("sys_t_dictionary dic on dic.dicKey = t.status")
                .leftJoin("pd_t_productitem pd on pd.classGuid = t.productItem");
        //.like("addr.tel", "1")
        //.le("a.province", "1");

        // 拼接参数
        if (StringUtils.hasLength(params.getParams().getProductCode())) {
            wrapper.like("productCode", params.getParams().getProductCode());
        }

        if (StringUtils.hasLength(params.getParams().getProductName())) {
            wrapper.like("productName", params.getParams().getProductName());
        }

        if (null != params.getParams().getStatus() && -1 != params.getParams().getStatus()) {
            wrapper.eq("t.status", params.getParams().getStatus());
        }


        if (null != params.getParams().getProductItem() && !"".equals(params.getParams().getProductItem())) {
            wrapper.eq("productItem", params.getParams().getProductItem());
        }

        // 获取分页数据
        IPage listPage = productMapper.selectJoinPage(new Page<>(params.getPage(), params.getLimit()), ProductVo.class, wrapper);

        return listPage;
    }

    @Override
    public List<Product> getProductList(PageParamsDto<Product> params) {
        return null;
    }
}
